﻿using Share;
using System.ComponentModel;
using UnityEngine;

namespace UIElement
{
    public delegate void OnSelectDelegate(CommonListItem item);
    public delegate void OnShowDelegate(CommonListItem item);

    public enum ScrollDir
    {
        [Description("纵向")]
        Protrait,
        [Description("横向")]
        Landscape,

    }
    /// <summary>
    /// 通用滚动显示基类
    /// </summary>
    public class ScrollBase : MonoBehaviour
    {

        [SerializeField]
        [Header("内容")] // 
        protected Transform m_Content;


        [SerializeField]
        [Header("元素模板")]
        protected GameObject m_ItemProfab; // 元素模版

        #region 个数
        int m_ItemNum = 0; //元素个数
        public int ItemNum
        {
            get { return m_ItemNum; }
            set { m_ItemNum = value; }
        }
        #endregion

        // 滚动方向，横向还是纵向
        [SerializeField]
        [Header("横向还是纵向")]
        protected ScrollDir m_ScrollDir = ScrollDir.Protrait;

        protected OnShowDelegate OnShowItem;
        protected OnSelectDelegate OnSelectItem;
        public virtual void DoInit(int num, OnShowDelegate onShowItem, OnSelectDelegate onSelectItem)
        {
            OnShowItem = onShowItem;
            OnSelectItem = onSelectItem;
            InitItemProfabs();
            SetCount(num);
        }



        public virtual void SetCount(int count)
        {
            ItemNum = count;
        }

        protected virtual void InitItemProfabs()
        {

            if (m_ItemProfab == null)
            {
                XKLog.LogError($"致命错误!元素模板必须要填 {gameObject.name}");
                return;
            }

        }

        protected GameObject CreateItem()
        {
            var item = ObjectMgr.Instance.CreateObj(m_ItemProfab, m_Content.transform);
            var cli = item.GetOrAddComponent<CommonListItem>();
            cli.Init(OnSelectItem, OnShowItem);

            return item;
        }
    }
}